package com.allwees.core.common.util.http;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;

/**
 * JDK的URLDecoder.decode会把'+'解码为空格，此工具类解决此问题
 *
 * @author yangylsky
 * @since 2019-11-06
 *
 */
public class UrlUtil {
	private UrlUtil() {
	}

	public static String encodeUrl(String url) {
		try {
			return URLEncoder.encode(url, StandardCharsets.UTF_8.name());
		} catch(UnsupportedEncodingException e) {
			return url;
		}
	}

	public static String decodeUrl(String url) {
		try {
			url = url.replace("+", "%2B");
			return URLDecoder.decode(url, StandardCharsets.UTF_8.name());
		} catch(UnsupportedEncodingException e) {
			return url;
		}
	}
}
